package com.lb.activity.rest;

import com.lb.activity.po.Dept;
import com.lb.activity.service.DeptService;
import com.lb.activity.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * @author liubin
 */
@RestController
@Api(value = "部门管理")
@RequestMapping("dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/toDept")
    @ApiOperation(value = "跳转到部门列表")
    public ModelAndView dept(){
        return new ModelAndView("dept/dept");
    }

    /**
     * 部门列表
     * @return
     */
    @GetMapping("depts")
    @ApiOperation(value = "部门列表")
    public Object menus(){
        List<Dept> depts= deptService.AllDepts();
        String json="";
        try {
            json = JsonUtil.objectToJson(depts).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        json = json.replaceAll("id", "menuId");
        return json;
    }

    @PostMapping("addDept")
    @ApiOperation(value = "部门添加")
    public Object addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @PostMapping("updateDept")
    @ApiOperation(value = "部门更新")
    public Object updateDept(Dept dept){
        return deptService.updateDept(dept);
    }

    @PostMapping("removeDept")
    @ApiOperation(value = "部门删除")
    public Object removeDept(Long id) {
        return deptService.removeDept(id);
    }

    /**
     * 查询该部门名字是否存在
     * @param deptName
     * @return
     */
    @GetMapping("/repet")
    @ApiOperation(value = "判断部门名称是否已经存在")
    public Object repet(String deptName) {
        int count = deptService.repet(deptName);
        if(count != 0) {
            return false;
        }
        return true;
    }

    @GetMapping("getById")
    @ApiOperation(value = "获取单个部门")
    public Object getById(Long id) {
        return deptService.getById(id);
    }

    @GetMapping("deptTree")
    @ApiOperation(value = "获取部门树状结构")
    public Object deptTree(){
        return deptService.deptTreeJson();
    }

}
